package de.fhd.pms.ctrl;

import java.util.Date;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import de.fhd.pms.dao.DeleteException;
import de.fhd.pms.dao.StationDao;
import de.fhd.pms.model.Auto;
import de.fhd.pms.model.Station;

/**
 * The Controller class for {@link Station} objects. Its tasks:
 * <ul>
 * <li>read and verify the parameters</li>
 * <li>access to the objects of the system</li>
 * <li>selection of the view and provision of content for the view</li>
 * </ul>
 * 
 * @author Josef Fischer
 * @author Alexander Neetzel
 * @author Philipp Six
 */
@Controller
public class StationController {
	private static Logger log = Logger.getLogger(StationController.class);

	private StationDao stationDao = null;

	public void setStationDao(StationDao stationDao) {
		this.stationDao = stationDao;
	}

	/**
	 * <p>
	 * Set a {@link Station} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/SetStation.html'.
	 * </p>
	 * 
	 * @param id
	 * @return View name 'admin-set-station' selecting view
	 *         '/view/admin-set-station.jsp'
	 */
	@RequestMapping(value = "/SetStation.html", method = RequestMethod.GET)
	public ModelAndView set(@RequestParam(required = false) Integer id) {
		log.debug("SetStation.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Station());
		} else {
			mv.addObject(stationDao.findById(id));
		}
		mv.setViewName("admin-set-station");
		return mv;
	}

	/**
	 * <p>
	 * Saves a {@link Station} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/SaveStation.html'.
	 * </p>
	 * 
	 * @param station
	 * @param model
	 * @return Redirect to '/AllStation.html'
	 */
	@RequestMapping(value = "/SaveStation.html", method = RequestMethod.POST)
	public String save(Station station, Model model) {
		if (station.getCreated() == null) {
			station.setCreated(new Date());
		}
		Station result = stationDao.save(station);

		if (station.getId() == null) {
			station.setId(result.getId());
		}
		model.addAttribute("statusMessageKey", "station.form.msg.success");
		return "redirect:AllStation.html";
	}

	/**
	 * <p>
	 * Deletes a {@link Station} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/DeleteStation.html'.
	 * </p>
	 * 
	 * @param station
	 * @throws Delete
	 *             Exception if the specific {@link Station} object contained
	 *             {@link Auto} objects.
	 * @return Redirect to '/AllStation.html'
	 */
	@RequestMapping(value = "/DeleteStation.html", method = RequestMethod.GET)
	public ModelAndView delete(Station station) {
		try {
			stationDao.delete(station);
			return new ModelAndView("redirect:AllStation.html");
		} catch (DeleteException e) {
			ModelAndView mv = new ModelAndView("admin-error");
			mv.addObject("message", e.getMessage());
			return mv;
		}

	}

	/**
	 * <p>
	 * Searches for all {@link Station} objects and returns them in a
	 * <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/AllStation.html'.
	 * </p>
	 * 
	 * @return View name 'admin-list-station' selecting view
	 *         '/view/admin-list-station.jsp'.
	 */
	@RequestMapping(value = "/AllStation.html", method = RequestMethod.GET)
	public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Alle Stationen");
		mv.addObject("message",
				"Sie können hier die Stationen bearbeiten oder löschen.");
		mv.addObject("stationen", stationDao.findAll());
		mv.addObject("google", stationDao.getGoogle());
		mv.setViewName("admin-list-station");
		return mv;
	}

	/**
	 * <p>
	 * Searches for all {@link Station} objects and returns them in a
	 * <code>Collection</code> for the customer.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/KundeStation.html'.
	 * </p>
	 * 
	 * @return View name 'kunde-list-station' selecting view
	 *         '/view/kunde-list-station.jsp'.
	 */
	@RequestMapping(value = "/KundeStation.html", method = RequestMethod.GET)
	public ModelAndView findAllKundeStation() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Die Stationen in München");
		mv.addObject(
				"message",
				"Die Fahrzeuge sind auf diese Stationen verteilt geparkt. Sie können Fahrzeuge aller Stationen nutzen.");
		mv.addObject("stationen", stationDao.findAll());
		mv.addObject("google", stationDao.getGoogle());
		mv.setViewName("kunde-list-station");
		return mv;
	}

}
